<?php
class Permissions_m extends MY_Model{
	
	private $_groups = array();
	
	function __construct(){
		parent::__construct();
		$this->_table = 'ser_permissions';
	}
	
	public function get_group($group_id)
	{
		// Save a query if you can
		if (isset($this->_groups[$group_id]))
		{
			return $this->_groups[$group_id];
		}
		$result = $this->db
			->where('group_id', $group_id)
			->get('ser_permissions')
			->result();
		$rules = array();

		foreach ($result as $row)
		{
			$rules[$row->module] = $row->roles ? json_decode($row->roles, true) : true;
		}
		$this->_groups[$group_id] = $rules;
		return $rules;
	}
	//lay tat ca module
	public function get_module_all($status = NULL){
		$this->db->order_by('ordering');
		$this->db->select('id as module');
		if(!empty($status))
			$this->db->where('status',$status);
		$result = $this->db->get('modules')->result();
		$rules = array();

		foreach ($result as $row)
		{
			$rules[$row->module] =  0;
		}
		return $rules;
	}
	
	public function insert($group_id, $data){
		$modules = $data['module'];
		//Array ( [module] => Array ( [0] => slider [1] => gallery [2] => news [3] => page ) ) 
		//xoa het
		$this->db->where('group_id',$group_id);
		$this->db->delete($this->_table);
		//them 
		$add['group_id'] = $group_id;
		foreach($modules as $mod){
				$add['module'] = $mod;
				parent::insert($add);
		}
		//xoa cache
	}
}